package fi.iki.joker.mobilogger;

import android.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import fi.iki.joker.mobilogger.db.DBUtil;
import fi.iki.joker.mobilogger.util.AlertUtil;

/**
 * This application is an entry point to Mobilogger. Keep clean.
 * @author juhak
 *
 */

public class MobiLoggerMain extends Activity {
	
	private static final String TAG = "MobiLoggerMain";
	private static final int MEMO_REQUEST = 1;
	private static final int BLOG_REQUEST = 2;
	private static final int LIST_BLOG_REQUEST = 3;
	private static final int BLOGCONFIG_REQUEST = 4;
	private static final int SETTINGS_REQUEST = 5;
	private static final int IMAGEREPO_REQUEST = 6;

    /** 
     * Called when the activity is first created. Makes sure that the database is ok and
     * then loads the main page if it is. 
     */
	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        DBUtil dbconn = new DBUtil();
        if(dbconn.testDatabaseReady(this)) {
        	Log.d(TAG, "The database is ready...");
        } else {
        	Log.e(TAG, "Failed to make the database ready.");
        	AlertUtil.showAlert(this,"Error", 
        			"Failed to start the application, cause: Cannot init DB.");
        	finish();
        }
        setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT);
        setContentView(fi.iki.joker.mobilogger.R.layout.main);
	}
	
	
	
	/** Called when framework creates the options menu */
	@Override
	public boolean onCreateOptionsMenu (Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		MenuItem item = menu.add(0, Menu.FIRST,Menu.NONE,fi.iki.joker.mobilogger.R.string.main_first_option);
		item.setAlphabeticShortcut('m');
		item = menu.add(0, 2, Menu.NONE, fi.iki.joker.mobilogger.R.string.main_second_option);
		item.setAlphabeticShortcut('b');
		SubMenu configMenu = menu.addSubMenu(0, 3, Menu.NONE, fi.iki.joker.mobilogger.R.string.main_third_option);
		item=configMenu.add(1,8, Menu.NONE,fi.iki.joker.mobilogger.R.string.main_config_option_blog);
		item.setAlphabeticShortcut('c');
		item=configMenu.add(1,9, Menu.NONE,fi.iki.joker.mobilogger.R.string.main_config_option_images);
		item=configMenu.add(1,10, Menu.NONE,fi.iki.joker.mobilogger.R.string.main_config_option_videos);
		item = menu.add(0, 4, Menu.NONE, fi.iki.joker.mobilogger.R.string.main_fourth_option);
		item.setAlphabeticShortcut('d');
		item = menu.add(0, 5, Menu.NONE, fi.iki.joker.mobilogger.R.string.main_fifth_option);
		item.setAlphabeticShortcut('s');
		item = menu.add(0, 6, Menu.NONE, fi.iki.joker.mobilogger.R.string.main_sixth_option);
		item.setAlphabeticShortcut('e');
		return result;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case Menu.FIRST:
        	newMemo();
            break;
        case 2:
        	newBlogEntry();
        	break;
        case 3:
        	//select the submenu;
        	break;
        case 4:
        	manageData();
        	break;
        case 5:
        	manageSettings();
        	break;
        case 6:
        	this.finish();
        	break;
        case 7:
        	manageData();
        	break;
        case 8:
        	newBlogConfig();
        	break;
        case 9:
        	newImageRepoConfig();
        	break;
        case 10:
        	break;
        }
        return super.onOptionsItemSelected(item);
    }
	
	/** 
	 * Start the memo editor Activity
	 */
	
	private final void newMemo() {
		Intent i = new Intent(this, MemoEditor.class);
		i.setAction(Intent.ACTION_INSERT);
		Uri cURI = null;
		cURI = Uri.parse("content://fi.iki.joker.mobilogger/memo");
		i.setData(cURI);
		startActivityForResult(i,MEMO_REQUEST);
	}
	
	/**
	 * Start the blog editor Activity
	 */
	private final void newBlogEntry() {
		Intent i = new Intent(this, BlogEntryEditor.class);
		i.setAction(Intent.ACTION_INSERT);
		Uri cURI = null;
		cURI = Uri.parse("content://fi.iki.joker.mobilogger/blogentry");
		i.setData(cURI);
		startActivityForResult(i,BLOG_REQUEST);
	}
	
	/**
	 * Start the blog list Activity
	 */
	private final void manageData() {
		Intent i = new Intent(this, ListMemos.class);
		i.setAction(Intent.ACTION_VIEW);
		Uri cURI = null;
		cURI = Uri.parse("not_really_used");
		i.setData(cURI);
		startActivityForResult(i,LIST_BLOG_REQUEST);
	}
	
	/**
	 * Start the activity to manage system settings
	 */
	
	private final void manageSettings() {
		Intent i = new Intent(this, MobiLoggerSettings.class);
		i.setAction(Intent.ACTION_VIEW);
		Uri cURI = null;
		cURI = Uri.parse("not_really_used");
		i.setData(cURI);
		startActivityForResult(i,SETTINGS_REQUEST);
	}
	
	/**
	 * Start the new blog config Activity
	 */
	private final void newBlogConfig() {
		Intent i = new Intent(this, BlogConfigEditor.class);
		i.setAction(Intent.ACTION_INSERT);
		Uri cURI = null;
		cURI = Uri.parse("content://fi.iki.joker.mobilogger/blogconfig");
		i.setData(cURI);
		startActivityForResult(i,BLOGCONFIG_REQUEST);
	}
	
	/**
	 * Start the image repository config activity
	 */
	
	private final void newImageRepoConfig() {
		Intent i = new Intent(this, ImageRepoConfigEditor.class);
		i.setAction(Intent.ACTION_INSERT);
		Uri cURI = null;
		cURI = Uri.parse("content://fi.iki.joker.mobilogger/imagerepository");
		i.setData(cURI);
		startActivityForResult(i,IMAGEREPO_REQUEST);
	}
}